# -*- coding:utf-8 -*-
import csv
import matplotlib.pyplot as plt
import numpy as np
def func1():
    # 这两行代码解决 plt 中文显示的问题
    csvfile = open("csv_test4.csv", "r")
    thedata = csvfile.read()
    csvfile.close()
    list1 = thedata.split("\n")
    # 初始化柱状图信息
    numberFromData = [0, 0, 0, 0, 0, 0]
    for i in list1:
        list2 = i.split(",")
        #print(len(list2))
        if(len(list2) == 8):
            if(list2[3] == '小于50人'):
                numberFromData[0] += 1
            elif(list2[3] == '50-99人'):
                numberFromData[1] += 1
            elif(list2[3] == '100-499人'):
                numberFromData[2] += 1
            elif(list2[3] == '500-999人'):
                numberFromData[3] += 1
            elif(list2[3] == '1000-4999人'):
                numberFromData[4] += 1
            else:
                numberFromData[5] += 1
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    numberFrom = ('小于50人', '50-99人', '100-499人', '500-999人', '1000-4999人','未标明')
    plt.bar(numberFrom, numberFromData)
    plt.title('公司人数规模分布情况')
    plt.show()

#公司类型饼状图
def func2():
    csvfile = open("csv_test4.csv", "r")
    thedata = csvfile.read()
    csvfile.close()
    labels = '有限责任公司(法人独资)','其他有限责任公司','有限责任公司（自然人投资或控股的法人独资）','有限责任公司','股份有限公司(非上市、国有控股)','有限责任公司（非自然人投资或控股的法人独资）','有限责任公司(外商投资企业与内资合资)','有限责任公司(自然人投资或控股)','股份有限公司','有限责任公司(外商投资企业法人独资)','有限责任公司(中外合资)','股份有限公司(非上市、自然人投资或控股)','有限责任公司（港澳台投资、非独资）','未标明'


    ll1 = (thedata.split('\n'))
    lls = []
    for i in ll1:
        llls = i.split(',')
        if(len(llls) == 8 and llls[0] != '公司名'):
            lls.append(llls[5])
    # 初始化柱状图信息
    companyTypeData = [0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,0, 0]
    #对柱状图数据赋值
    for i in lls:
        if (i == '有限责任公司(法人独资)'):
            companyTypeData[0] += 1
        elif (i == '其他有限责任公司'):
            companyTypeData[1] += 1
        elif (i == '有限责任公司（自然人投资或控股的法人独资）'):
            companyTypeData[2] += 1
        elif (i == '有限责任公司'):
            companyTypeData[3] += 1
        elif (i == '股份有限公司(非上市、国有控股)'):
            companyTypeData[4] += 1
        elif (i == '有限责任公司（非自然人投资或控股的法人独资）'):
            companyTypeData[5] += 1
        elif (i == '有限责任公司(外商投资企业与内资合资)'):
            companyTypeData[6] += 1
        elif (i == '有限责任公司(自然人投资或控股)'):
            companyTypeData[7] += 1
        elif (i == '股份有限公司'):
            companyTypeData[8] += 1
        elif(i == '有限责任公司(外商投资企业法人独资)'):
            companyTypeData[9] += 1
        elif(i == '有限责任公司(中外合资)'):
            companyTypeData[10] += 1
        elif(i == '股份有限公司(非上市、自然人投资或控股)'):
            companyTypeData[11] += 1
        elif(i == '有限责任公司（港澳台投资、非独资）'):
            companyTypeData[12] += 1
        else:
            companyTypeData[13] += 1
    #使能够正常显示中文
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    fraces = companyTypeData
    explode = [0, 0.1, 0, 0,0,0,0,0,0,0,0,0,0,0]
    plt.axes(aspect=1)
    plt.pie(x=fraces, labels=labels, autopct='%0f%%', explode=explode, shadow=True)
    plt.show()



def func3():
    csvfile = open("csv_test4.csv", "r")
    thedata = csvfile.read()
    csvfile.close()
    ll1 = (thedata.split('\n'))
    lls = []
    lllss = []
    for i in ll1:
        llls = i.split(',')
        lllss.append(llls)
        if (len(llls) == 8 and llls[7] != "公司注册资金"):
            lls.append(llls[7])
    list = []
    for i in lls:
        tmp = ''
        for j in i:
            if(j == '万'):
                break
            else:
                tmp = tmp + j
        list.append(eval(tmp))
    list1 = []
    for i in range(len(list)):
        tmp = (lllss[i + 1][0],list[i])
        list1.append(tmp)
    for i in range(len(list1)):
        for j in range(len(list1) - i - 1):
            if(list1[j][1] > list1[j+1][1]):
                tmp = list1[j]
                list1[j] = list1[j+1]
                list1[j+1] = tmp
    csvfile = open("排序后的表.csv", "w", newline="")
    writer = csv.writer(csvfile)
    writer.writerow(('公司名','公司注册资金（万元）'))
    writer.writerows(list1)
    csvfile.close()


def func4():
    csvfile = open("csv_test4.csv", "r")
    thedata = csvfile.read()
    csvfile.close()
    ll1 = (thedata.split('\n'))
    lls = []
    for i in ll1:
        llls = i.split(',')
        if (len(llls) == 8 and llls[6] != "公司地址"):
            lls.append(llls[6])
    #北京
    ss = set()
    for i in lls:
        t = (i[0:2])
        ss.add(t)
    ss.remove("中国")
    companyCities = []
    for i in ss:
        companyCities.append(i)
    companyCityData = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    for i in lls:
        for j in range(len(companyCityData)):
            if(companyCities[j] in i):
                companyCityData[j] += 1
                break
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    plt.bar(companyCities, companyCityData)
    plt.title('公司地址分布情况')
    plt.show()



func4()
